<template>
  <div>
    <el-form :model="dataForm" ref="dataForm">
      <el-form-item>
        <el-button type="primary" @click="openSearch(1)">查询</el-button>
        <el-button type="primary" @click="_export()">导出</el-button>
        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <search ref="search" @onSearched="onSearch"/>
    <el-table
      :data="dataList"
      border
      style="width: 100%;"
      @selection-change="selectionChangeHandle"
    >
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column align="center" type="index" width="80" label="序号" />
      <el-table-column prop="deviceName" label="巡查设备" align="center" />
      <el-table-column prop="areaName" label="巡查区域" align="center" />
      <el-table-column prop="areaUserName" label="区域负责人" align="center" />
<!--      <el-table-column prop="deviceStatus" label="设备状态" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tag v-if="scope.row.deviceStatus === 1">在用</el-tag>-->
<!--          <el-tag v-if="scope.row.deviceStatus === 2">闲置</el-tag>-->
<!--          <el-tag v-if="scope.row.deviceStatus === 3">维修</el-tag>-->
<!--          <el-tag v-if="scope.row.deviceStatus === 4">借入</el-tag>-->
<!--          <el-tag v-if="scope.row.deviceStatus === 5">借出</el-tag>-->
<!--          <el-tag v-if="scope.row.deviceStatus === 6">报废</el-tag>-->
<!--          <el-tag v-if="scope.row.deviceStatus === 7">其他</el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="comments" label="巡查数据" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          <el-tooltip class="item" placement="top" effect="light">-->
<!--            <div slot="content" style="width: 80px;">-->
<!--              <span>{{scope.row.comments || '暂无数据'}}</span>-->
<!--            </div>-->
<!--            <el-tag>情况</el-tag>-->
<!--          </el-tooltip>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column prop="comments" label="巡查意见" align="center" />
      <el-table-column prop="realname" label="设备负责人" align="center" />
      <el-table-column prop="createTime" label="巡查上报时间" align="center" />
      <el-table-column fixed="right" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <patrol-add-or-update ref="patrolAddOrUpdate"></patrol-add-or-update>
  </div>
</template>

<script>
  import PatrolAddOrUpdate from './region/patrol-add-or-update'
  import Search from './region/search'
  export default {
    name: 'patrol',
    data() {
      return {
        dataList: [],
        loading: false,
        dataForm: {},
        dataTime: [],
        startTime: '',
        endTime: '',
        pageIndex: 1,
        dataListSelections: [],
        totalPage: 0,
        pageSize: 10
      }
    },
    components: { PatrolAddOrUpdate, Search },
    mounted() {
      this.getDataList()
    },
    methods: {
      _export() {
        window.location = this.$http.downloadUrl('/device/area/patrol/record/exportDeviceAreaPatrolRecord')
      },
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      openSearch (atorlStatus) {
        this.$refs.search.open(atorlStatus)
      },
      onSearch(searchForm) {
        this.pageIndex = 1
        this.totalCount = 0
        this.getDataList(searchForm)
      },
      async getDataList(searchForm = {}) {
        var requestData = Object.assign(searchForm, {
          page: this.pageIndex,
          limit: this.pageSize
        })
        var res = await this.$http.post('/device/area/patrol/record/page', requestData)

        this.dataList = res.data.list
        this.totalPage = res.data.totalCount
      },
      addOrUpdate(id) {
        this.$nextTick(() => this.$refs.patrolAddOrUpdate.init(id))
      },
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      async deleteHandle(id) {
        var ids = id || this.dataListSelections.map(item => item.id).join(',')
        var confirm = await this.$confirm(`确定要进行删除操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        if (confirm !== 'confirm') return

        let res = await this.$http({
          url: '/device/area/patrol/record/delete',
          method: 'post',
          data: {
            ids: ids
          }
        })
        if (res && res.code === 200) {
          this.$notify.success({ title: '成功', message: '删除成功' })
          this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex --
          this.getDataList()
        } else {
          this.$notify.error({ title: '错误', message: res.msg })
        }
      }
    }
  }
</script>

<style scoped>

</style>
